<?php

namespace App\Backend\Http\Controllers;

use App\Banner;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class BannerController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index($type)
    {
        $data['banner']=Banner::where('type',$type)->orderBy('id','desc')->paginate(5);
        $data['type'] = $type;
        switch ($type){
            //全部广播
            case "1":
                return view('backend::banner.radio',$data);
            //每周推荐
            case "2":
                return view('backend::banner.weekWelfare',$data);
            //底部广告
            case "3":
                return view('backend::banner.bottomad',$data);
            //默认首页Banner
            default:
                return view('backend::banner.banner',$data);
        }
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($type,$id)
    {
        $data['banner'] = Banner::find($id);
        $data['type'] = $type;
        switch ($type){
            //全部广播
            case "1":
                return view('backend::banner.radiocreate',$data);
            //每周推荐
            case "2":
                return view('backend::banner.weekWelfarecreate',$data);
            //底部广告
            case "3":
                return view('backend::banner.bottomadcreate',$data);
            //默认首页Banner
            default:
                return view('backend::banner.create',$data);
        }
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'title' => 'required|string|min:2',
            'type' => 'required',
            'image' => 'sometimes|required|string',
            'sort' => 'sometimes|required',
            'is_show' => 'sometimes|required',
            'url' => 'sometimes|required|url',
            'published_at' => 'sometimes|required',
        ]);
        if(request('id')){
            Banner::where('id',request('id'))->update($data);
        }else{
            Banner::create($data);
        }
        return redirect('/backend/banner/'.$data['type']);
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::banner.banner');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($type,$id)
    {
        Banner::where('id',$id)->delete();
        return redirect('/backend/banner/'.$type);
    }
}
